package com.shievy.anweb.webview.web.activity

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import com.shievy.anweb.base.loadsir.ErrorCallback
import com.shievy.anweb.base.loadsir.LoadingCallback
import com.shievy.anweb.base.uitl.LogUtil
import com.shievy.anweb.webview.R
import com.shievy.anweb.webview.web.callback.WebResultCallBack
import com.shievy.anweb.webview.web.callback.WebViewCallBack
import com.shievy.anweb.webview.web.chrome.XWebChromeClient
import com.shievy.anweb.webview.web.client.XWebViewClient
import com.shievy.anweb.webview.databinding.FragmentWebviewBinding
import com.shievy.anweb.webview.uitl.Constants

/**
 * 暴露的 WebView Fragment
 * */
class WebViewFragment private constructor() : Fragment(), WebViewCallBack {

    /**
     * 需要加载的URL
     * */
    private val url: String by lazy {
        arguments?.getString(Constants.WEB_URL, "") ?: ""
    }
    private lateinit var binding: FragmentWebviewBinding

    /**
     * 创建loading
     * */
    private lateinit var loadSir: LoadService<*>

    private val client: XWebViewClient by lazy { XWebViewClient(this@WebViewFragment) }
    private val chrome: XWebChromeClient by lazy { XWebChromeClient(this@WebViewFragment) }

    /**
     * 页面加载是否出错
     * */
    private var isLoadError = false

    companion object {
        fun createWebViewFragment(url: String, bundle: Bundle = Bundle()): WebViewFragment {
            return WebViewFragment().apply {
                bundle.putString(Constants.WEB_URL, url)
                arguments = bundle
            }
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_webview, container, false)
        binding.webView.registerWebViewCallBack(this)
        loadSir = LoadSir.getDefault().register(binding.webView) {
//            重新加载操作
            loadSir.showCallback(LoadingCallback::class.java)
            binding.webView.reload()
        }
        binding.webView.loadUrl(url)
        return loadSir.loadLayout
    }

    override fun onPageStart(url: String?) {
        LogUtil.e(msg = "onPageStart url $url")
        url?.let {
            loadSir.showCallback(LoadingCallback::class.java)
        }
    }

    override fun onPageFinished(url: String?) {
        url?.let {
            if (!isLoadError) {
                loadSir.showSuccess()
            } else {
                loadSir.showCallback(ErrorCallback::class.java)
            }
        }
        isLoadError = false
    }

    override fun onPageError(url: String?) {
        isLoadError = true

    }

    override fun onChangeTitle(title: String?) {
        if (requireActivity() is WebResultCallBack) {
            (requireActivity() as WebResultCallBack).onChangeTitle(title)
        }
    }

}